﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows.Threading;
using BrainTechLLC;

namespace ImageAppViewModels
{
    public partial class FileImageCollectionViewModel : ObservableCollection<FileImageViewModel>, IWatcherNotifiable
    {
        public bool _firstTimeProcessing = true;

        private void LazyLoad()
        {
            try
            {
                for (int n = 0; n < _directoriesToExamine.Count; n++)
                {
                    if (_stopLazyLoad)
                    {
                        break;
                    }

                    var path = _directoriesToExamine[n].Directory;
                    AddByPathNames(_pathDoesNotExist, _pathsDoNotExist, path);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            while (true && !_stopLazyLoad)
            {
                if (_stopLazyLoad)
                {
                    break;
                }

                int didSomething = 0;
                try
                {
                    if (!_needRefresh)
                    {
                        _firstTimeProcessing = true;
                    }

                    while (!_needRefresh)
                    {
                        if (_stopLazyLoad)
                        {
                            break;
                        }

                        if (_firstTimeProcessing)
                        {
                            _firstTimeProcessing = false;
                            Thread.Sleep(10);
                        }
                        else
                        {
                            Thread.Sleep(30);
                        }
                    }

                    if (_stopLazyLoad)
                    {
                        break;
                    }

                    while (_fileImageCollection.Count > 0 || _needRefresh)
                    {
                        Console.WriteLine("Lazy Load File Image Collections: " + _fileImageCollection.Count.ToString());
                        _dispatcher.BeginInvoke(new VoidDelegate(() =>
                        {
                            try
                            {
                                if (_needRefresh)
                                {
                                    ClearAll();
                                    _fileImageCollection.ClearAllItems();
                                    _needRefresh = false;
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                        }),
                        DispatcherPriority.Send); // was background

                        Thread.Sleep(Math.Max(0, 20 - didSomething)); // was 50

                        if (_fileImageCollection.Count > 0 && !_needRefresh)
                        {
                            _needRefresh = true;
                            didSomething++;
                        }
                        else
                        {
                            if (_fileImageCollection.Count == 0)
                            {
                                didSomething = 0;
                            }
                        }
                    }

                    _backgroundWorker.Priority = ThreadPriority.Normal;

                    for (int n = 0; n < _directoriesToExamine.Count; n++)
                    {
                        var path = _directoriesToExamine[n].Directory;
                        AddByPathNames(_pathDoesNotExist, _pathsDoNotExist, path);
                        // Thread.Sleep(10);
                        if (_fileImageCollection.Count > 100)
                        {
                            _backgroundWorker.Priority = ThreadPriority.BelowNormal;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }            
        }
    }
}
